package com.egreen.order.service.api.impl;

import com.egreen.order.dao.RecyclableNameDao;
import com.egreen.order.dao.RecyclableTypeDao;
import com.egreen.common.pojo.entity.order.RecyclableName;
import com.egreen.common.pojo.entity.order.RecyclableType;
import com.egreen.order.service.IRecyclableNameService;
import com.egreen.order.service.IRecyclableTypeService;
import com.egreen.common.pojo.DTO.order.AddRecyclableNameDTO;
import com.egreen.common.serviceapi.order.IRecyclableServiceAPI;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @Author: lyy
 * @Version: 1.0
 * @JDK-version: 1.8
 * @Date: 2022/7/27 9:37
 * @Description:
 */
public class RecyclableServiceAPIImpl implements IRecyclableServiceAPI {
    @Autowired
    IRecyclableTypeService recyclableTypeService;
    @Autowired
    RecyclableTypeDao recyclableTypeDao;
    @Autowired
    IRecyclableNameService recyclableNameService;
    @Autowired
    RecyclableNameDao recyclableNameDao;

    /**
     * 可回收物品类型的增加
     * @param typeName
     */
    @Override
    public void addRecyclableType(String typeName) {
        //通过查询RecyclableType获取可回收物品的类型
        RecyclableType recyclableType = new RecyclableType(null,typeName);
        //遍历数据库并且将数据存入List集合中
        List<RecyclableType> recyclableTypes = this.recyclableTypeService.list();
        for (int i = 0; i < recyclableTypes.size(); i++) {
            //判断在数据库中是否还有这个可回收物品的类型(有:提示有了;没有:增加)
            if (recyclableTypes.get(i).getTypeName().equals(typeName)){
                //TODO 抛出一个类型重复异常
                System.out.println("已存在");
                return;
            }
        }
        recyclableTypeService.save(recyclableType);
    }

    /**
     * 可回收物品类型的删除
     * @param typeId
     */
    @Override
    public void removeRecyclableType(Integer typeId) {
        recyclableTypeDao.deleteById(typeId);
    }

    /**
     * 可回收物品的名称
     * @param addRecyclableNameDTO
     */
    @Override
    public void addRecyclableName(AddRecyclableNameDTO addRecyclableNameDTO) {
        //遍历数据库并且将数据存入List集合中
        List<RecyclableName> recyclableNames = this.recyclableNameService.list();
        for (int i = 0; i < recyclableNames.size(); i++) {
            //判断在数据库中是否还有这个可回收物品的类型(有:提示有了;没有:增加)
            if (recyclableNames.get(i).getName().equals(addRecyclableNameDTO.getName())){
                //TODO 抛出一个类型重复异常
                System.out.println("已存在");
                return;
            }
        }
        recyclableNameService.save(new RecyclableName(null, addRecyclableNameDTO.getTypeId(),
                addRecyclableNameDTO.getName(), addRecyclableNameDTO.getDescription(),
                addRecyclableNameDTO.getPrice()));
    }

    @Override
    public void removeRecyclableNameDTO(Integer id) {
        recyclableNameDao.deleteById(id);
    }



}
